
package cn.com.pusai.base.person.action;

import cn.com.pusai.base.person.model.*;
import cn.com.pusai.base.person.service.PersonService; //import cn.com.pusai.base.role.service.RoleService;
import cn.com.pusai.common.action.BaseAction;
import cn.com.pusai.common.config.SysProperties;
import cn.com.pusai.common.util.MethodUtil;
import cn.com.pusai.common.util.SearchMap;
import cn.com.pusai.core.page.PageResult;
import cn.com.pusai.core.util.SecurityUtil;
import cn.com.pusai.core.util.StringUtil;
import java.util.*;

public class PersonAction extends BaseAction {

	private PersonService personService;
	// private RoleService roleService;
	private Person person;
	private List roleList;
	private String id;
	private String roleId[];
	private String password[];

	public PersonAction() {
	}

	// public void setRoleService(RoleService roleService)
	// {
	// this.roleService = roleService;
	// }

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public String[] getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId[]) {
		this.roleId = roleId;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String[] getPassword() {
		return password;
	}

	public void setPassword(String password[]) {
		this.password = password;
	}

	public String listPerson() throws Exception {
		try {
			// SearchMap searchMap = new SearchMap();
			// searchMap.like("a.name", (new
			// StringBuilder("%")).append(StringUtil.trimToEmpty((String)getSearchValue().get("name"))).append("%").toString());
			// searchMap.like("a.login", (new
			// StringBuilder("%")).append(StringUtil.trimToEmpty((String)getSearchValue().get("login"))).append("%").toString());
			// searchMap.notEq("status", Person.STATUS_DISABLED);
			// searchMap.addOrder("a.account");
			// PageResult pageResult =
			// personService.findObjectsForPage(getPageNo(),
			// SysProperties.PAGESIZE, searchMap);
			// Person person;
			// String roleNames;
			// for (Iterator it = pageResult.getItems().iterator();
			// it.hasNext(); person.setRoleNames(roleNames))
			// {
			// person = (Person)it.next();
			// roleNames = "";
			// searchMap = new SearchMap();
			// searchMap.eq("id.personId", person.getId());
			// List personRoleList = personService.findObjects(searchMap,
			// cn.com.pusai.base.merge.model.PersonRole.class);
			// for (Iterator iterator = personRoleList.iterator();
			// iterator.hasNext();)
			// {
			// PersonRole personRole = (PersonRole)iterator.next();
			// Role role =
			// (Role)personService.findObjectById(personRole.getId().getRoleId(),
			// cn.com.ecinc.base.merge.model.);
			// if (role != null)
			// roleNames = (new
			// StringBuilder(String.valueOf(roleNames))).append(roleNames.equals("")
			// ? role.getName() : (new
			// StringBuilder(",")).append(role.getName()).toString()).toString();
			// }
			//
			// }

			// setPageResult(pageResult);
		} catch (Exception e) {
			handleException(e);
		}
		return "list";
	}

	public String initPerson() throws Exception {
		try {
		//	setPerson(new Person());
			// setRoleList(roleService.findRoles());
			
//		Person p=(Person)personService.findObjectById("4028cb8138f43dca0138f444d55c0003");
//		System.out.println("名称:"+p.getName());
//		System.out.println("编号:"+p.getAccount());
//		System.out.println("==:"+p.getEmail());
//		SearchMap search=new SearchMap();
//		search.eq("ACCOUNT", "zhulishan");
//		List list=personService.findObjects(search);
//		System.out.println("查询到的大小:"+list.size());

			
			
			Person p=(Person)personService.findObjectById("4028cb8138f43dca0138f444d55c0003");
			System.out.println("采用注解的方式:"+p.getName());
			System.out.println("名称:"+p.getName());
			System.out.println("编号:"+p.getAccount());
			System.out.println("==:"+p.getEmail());
			
			System.out.println("pageSize:"+SysProperties.Properties.getIntProperty("base.pageSize"));
			System.out.println("admin login:"+SysProperties.ADMIN_LOGIN);
			
			Person p1=new Person();
			p1.setName("aaa");
			p1.setAccount("aa1");
			personService.create(p1);
			
			
		} catch (Exception e) {
			handleException(e);
		}
		return "add";
	}

	public String viewPerson() throws Exception {
		try {
			if (getPerson() != null)
				id = String.valueOf(getPerson().getId());
			// List list = roleService.findRolesByPerson(id);
			// setRoleList(list);
			Person personVO = (Person) personService.findObjectById(id);
			setPerson(personVO);
		} catch (Exception e) {
			handleException(e);
		}
		return "view";
	}

	public String addPerson() throws Exception {
		List roleList = null;
		try {
			Person person = getPerson();
			if (roleId == null)
				roleId = new String[0];
			personService.createPerson(person, roleId);
			setRoleList(roleList);
			setActionStatus("success");
		} catch (Exception e) {
			handleException(e);
			setRoleList(roleList);
			return "add";
		}
		return viewPerson();
	}

	public String editPerson() throws Exception {
		try {
			if (getPerson() != null)
				id = String.valueOf(getPerson().getId());
			// List list = roleService.findRolesByPerson(id);
			// setRoleList(list);
			//Person personVO = (Person) personService.findObjectById(id);
			
			
			//setPerson(personVO);
		} catch (Exception e) {
			handleException(e);
		}
		return "edit";
	}

	public String modPerson() throws Exception {
		List roleList = null;
		try {
			Person fromWebVO = getPerson();
			if (fromWebVO != null)
				id = String.valueOf(fromWebVO.getId());
			if (password != null && !password[0].equals(""))
				fromWebVO.setPassword(SecurityUtil.md5Hex(password[0]));
			Person person = (Person) personService.findObjectById(fromWebVO
					.getId());
			person = (Person) MethodUtil.copyProperties(person, fromWebVO);
			// roleList = roleService.findRolesByPerson(id);
			if (roleId == null)
				roleId = new String[0];
			personService.updatePerson(person, roleId);
			setRoleList(roleList);
			setActionStatus("success");
		} catch (Exception e) {
			handleException(e);
			setRoleList(roleList);
			viewPerson();
			return "edit";
		}
		return viewPerson();
	}

	public String delPerson() throws Exception {
		try {
			String ids[] = getSelectedRow();
			if (ids == null) {
				ids = (new String[] { getId() });
				if (ids == null)
					ids = new String[0];
			}
			personService.deletePerson(ids);
		} catch (Exception e) {
			handleException(e);
		}
		return listPerson();
	}
}
